﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PlayDeezGames.Common;
using System.Xml.Linq;

namespace DungeonDelver.Engine
{
    public class MapCell : DescriptoredEntityBase
    {

        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, System.Xml.Linq.XElement theXElement)
            {
                return new MapCell(theParent as MapColumn, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new MapCell(theParent as MapColumn);
            }
            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.MapCellFactory, new MapCell.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.MapCellFactory);
            }
        }

        private MapCell(MapColumn parent)
            : base(parent)
        {
        }

        private MapCell(MapColumn parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }
        private Terrain terrain = null;
        public Terrain Terrain
        {
            get
            {
                return terrain;
            }
            set
            {
                if (terrain != null)
                {
                    terrain.ParentEntity = null;
                }
                terrain = value;
                if (terrain != null)
                {
                    if (terrain.ParentEntity != this)
                    {
                        terrain.ParentEntity = this;
                    }
                }
            }
        }
        private Item item = null;
        public Item Item
        {
            get
            {
                return item;
            }
            set
            {
                if (item != null)
                {
                    item.ParentEntity = null;
                }
                item = value;
                if (item != null)
                {
                    item.ParentEntity = this;
                }
            }
        }
        private Creature creature = null;
        public Creature Creature
        {
            get
            {
                return creature;
            }
            set
            {
                if (creature != null)
                {
                    creature.ParentEntity = null;
                }
                creature = value;
                if (creature != null)
                {
                    creature.ParentEntity = this;
                }
            }
        }
        public MapColumn MapColumn
        {
            get
            {
                return ParentEntity as MapColumn;
            }
        }
        public Game Game
        {
            get
            {
                return MapColumn != null ? MapColumn.Game : null;
            }
        }

        public int Row
        {
            get
            {
                if (MapColumn != null)
                {
                    foreach (var row in MapColumn.Rows)
                    {
                        if (MapColumn[row] == this)
                        {
                            return row;
                        }
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }


        protected override System.Xml.Linq.XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.MapCell);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.MapCellFactory));
            result.Add(new XAttribute(Names.EngineXNames.Row, Row.ToString()));
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            MapColumn[int.Parse(theXElement.Attribute(Names.EngineXNames.Row).Value)] = this;
            foreach (var child in Children)
            {
                if (child is Terrain)
                {
                    Terrain = child as Terrain;
                }
                else if (child is Item)
                {
                    Item = child as Item;
                }
                else if (child is Creature)
                {
                    Creature = child as Creature;
                }
            }
        }

        public static MapCell Create(MapColumn theParent)
        {
            return EntityFactories.Create(theParent, Names.EngineFactories.MapCellFactory) as MapCell;
        }
        public static MapCell Load(MapColumn theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as MapCell;
        }
    }
}
